import baseUrl from './config'
let BaceUrl = baseUrl.baseUrl
function setRequest(url, method, data) {
	let header = {
		"content-type": "application/x-www-form-urlencoded",
		"Authorization": ''
	}
	uni.showLoading();
	let token = uni.getStorageSync('token');
	if(token){
		header = {
			"Authorization": token,
			"content-type": "application/x-www-form-urlencoded"
		}
	}
	var promise = new Promise((resolve, reject) => {
		var that = this;
		uni.request({
			url: BaceUrl + url,
			data,
			method,
			header,
			success: function(res) {
				uni.hideLoading();
				if(res.data.code === 200){
					resolve(res.data);
				}else{
					resolve(res.data);
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					return
				}
			},
			fail: function(e) {
				uni.hideLoading();
				reject("网络出错,连接失败---" + JSON.stringify(e));
			}
		});
	});
	return promise;
}

function getRequest(url, data) {
	return setRequest(url, "GET", data);
}

function postRequest(url, data) {
	return setRequest(url, "POST", data);
}

function putRequest(url, data) {
	return setRequest(url, "PUT", data);
}

function deleteRequest(url, data) {
	return setRequest(url, "DELETE", data);
}

module.exports = {
	get: getRequest,
	post: postRequest,
	put: putRequest,
	delete: deleteRequest,
	baseUrl:baseUrl,
};
